<?php
namespace app\shop\model;
use app\common\model\CommonModel;
use app\common\model\OrderActivity;
use app\common\model\Activity;
use app\common\model\Shop;
use app\common\model\Goods;
use app\common\model\Stock;
use think\Session;
use think\Db;

class ActivityModel extends CommonModel
{
	/**
    * 添加活动信息
    */
    public static function setActivity($info)
    {
        $data['shop_id']    = Session::get('admin.shop_id');
        $data['act_type']   = $info['act_type'];
        $data['act_name']   = $info['act_name'];
        $data['start']      = strtotime($info['start']);
        $data['end']        = strtotime($info['end']);
        
        switch($info['act_type'])
        {
            case 0:$data['mz_money'] = $info['mz_money'];$data['yh_money'] = $info['yh_money'];break;
            case 1:$data['discount'] = $info['discount'];$data['number'] = $info['number'];$data['zs_goods'] = $info['zs_goods'];break;
            case 2:$data['mz_money'] = $info['mz_money'];$data['zs_goods'] = $info['zs_goods'];break;
            case 3:$data['discount'] = $info['discount'];$data['zs_goods'] = $info['zs_goods'];break;
        }
        
        if(!(new Activity())->serActivity($data))
		{
			return back(config('state.STATUS_CODE_TWO'),config('state.STATUS_MSG_TWO'));
		}
		return back(config('state.STATUS_CODE_ONE'),config('state.STATUS_MSG_ONE'));
    }
    /**
    * 获取活动列表信息
    */
    public static function activityList($info)
    {
       $where['act_type'] = $info['ty'];
       $data = (new Activity())->getActivityList($where,10,$info['page']);
       $Goods = new Goods();
       foreach($data['data'] as $i=>$vo)
       {
           $data['data'][$i]['time'] = date('Y-m-d',$vo['start']) . "~" . date('Y-m-d',$vo['end']);
           if($vo['act_type'] == 3)
           {
               $data['data'][$i]['zs_goods'] = $Goods->fieldGoodsColumn(['goods_id'=>['IN',$vo['zs_goods']]],'goods_name');
           }else{
               $data['data'][$i]['zs_goods'] = $Goods->fieldGoods(['goods_id'=>$vo['zs_goods']],'goods_name');
           }
       }
       return $data;
    }
    /**
    * 获取总部活动信息
    */
    public static function headactivity($info)
    {
        $where = [];
       /*店铺选择*/
		if($info['shopid'] != 0 && $info['shopid'])
		{
			$where['shop_id'] = $info['shopid'];
		}
       /*时间区间*/
        if($info['start'] != '' || $info['end'] != '')
		{
			if($info['start'])
			{
				$where['start'][] = ['>',strtotime($info['start'])];
			}
			if($info['end'])
			{
				$where['start'][] = ['<',strtotime($info['end'])];
			}
		}
        /*上月、当前月*/
        if($info['shijian'] != '')
        {
            if($info['shijian'] == CommonModel::NUMBER_ZERO)
            {
                $where['start'][] = ['>',mktime(0,0,0,date('m'),1,date('Y'))];
                $where['start'][] = ['<',mktime(23,59,59,date('m'),date('t'),date('Y'))];
            }else{
                $where['start'][] = ['>',mktime(0, 0, 0,date('m')-1,1,date('Y'))];
                $where['start'][] = ['<',mktime(0, 0, 0,date('m'),0,date('Y'))];
            }
        }
        
       $data = (new Activity())->getActivityList($where,10,$info['page']);
       $Goods = new Goods();
       foreach($data['data'] as $i=>$vo)
       {
           $data['data'][$i]['time'] = date('Y-m-d',$vo['start']) . "~" . date('Y-m-d',$vo['end']);
           $data['data'][$i]['shop_id'] = (new Shop())->shopField(['shop_id'=>$vo['shop_id']],'shop_name');
           
           $goodsname = '';
           if($vo['act_type'] == 3)
           {
               $gnamearr = $Goods->fieldGoodsColumn(['goods_id'=>['IN',$vo['zs_goods']]],'goods_name');
               $goodsname = implode("+", $gnamearr);
           }
           
           if($vo['act_type'] == 2)
           {
              $goodsname = $Goods->fieldGoods(['goods_id'=>$vo['zs_goods']],'goods_name'); 
           }
           $remarks = '';
           $actype = '';
           switch($vo['act_type'])
           {
               case 0:$actype = '满减活动';$remarks = "满{$vo['mz_money']};减{$vo['yh_money']}";break;
               case 1:$actype = '满折活动';$remarks = "满{$vo['number']}件;折扣{$vo['discount']}%";break;
               case 2:$actype = '满赠活动';$remarks = "满{$vo['mz_money']};赠{$goodsname}";break;
               case 3:$actype = '折扣活动';$remarks = "{$goodsname};折扣{$vo['discount']}%";break;
           }
           $data['data'][$i]['act_type'] = $actype;
           $data['data'][$i]['remarks'] = $remarks;
           
       }
       return $data;
    }
    /**
    * 活动详情
    */
    public static function headaInfo($info)
    {
        /*活动详情信息*/
       $actInfo = (new Activity())->ActivityList(['activity_id'=>$info['id']],'','find');
       $actInfo['time'] = date('Y-m-d',$actInfo['start']) . "~" . date('Y-m-d',$actInfo['end']);
       $actInfo['shop_id'] = (new Shop())->shopField(['shop_id'=>$actInfo['shop_id']],'shop_name');
       $goodsname = '';
       $Goods = new Goods();
       if($actInfo['act_type'] == 3)
       {
           $gnamearr = $Goods->fieldGoodsColumn(['goods_id'=>['IN',$actInfo['zs_goods']]],'goods_name');
           $goodsname = implode("+", $gnamearr);
       }
       if($actInfo['act_type'] == 2)
       {
          $goodsname = $Goods->fieldGoods(['goods_id'=>$actInfo['zs_goods']],'goods_name'); 
       }
       $remarks = '';
       $actype = '';
       switch($actInfo['act_type'])
       {
           case 0:$actype = '满减活动';$remarks = "满{$actInfo['mz_money']};减{$actInfo['yh_money']}";break;
           case 1:$actype = '满折活动';$remarks = "满{$actInfo['number']}件;折扣{$actInfo['discount']}%";break;
           case 2:$actype = '满赠活动';$remarks = "满{$actInfo['mz_money']};赠{$goodsname}";break;
           case 3:$actype = '折扣活动';$remarks = "{$goodsname};折扣{$actInfo['discount']}%";break;
       }
       $actInfo['act_type'] = $actype;
       $actInfo['remarks'] = $remarks;
       $data[] = empty($actInfo) ? array():$actInfo->toArray();
       $OrderActivity = new OrderActivity();
        /*客流量（人）*/
        $data[] = $OrderActivity->actOrderCount($info['id'],1);
        /*销售订单数（单）*/
        $data[] = $OrderActivity->actOrderCount($info['id'],2);
        /*销售总额（元）*/
        $data[] = $OrderActivity->actOrderCount($info['id'],3);
        /*销售总成本（元）*/
        $data[] = $OrderActivity->actOrderCount($info['id'],4);
        /*销售总收入（元）*/
        $data[] = $OrderActivity->actOrderCount($info['id'],5);
        return $data;
    }
    /**
    * 活动类型
    */
    private function acttype($ty)
    {
        switch($ty)
        {
            case 0:return '满减活动';break;
            case 1:return '满折活动';break;
            case 2:return '满赠活动';break;
            case 3:return '折扣活动';break;
        }    
    }
    /**
    * 编辑活动信息
    */
    public static function editActivity($info)
    {
        $data['act_name']   = $info['act_name'];
        $data['start']      = strtotime($info['start']);
        $data['end']        = strtotime($info['end']);
        
        switch($info['act_type'])
        {
            case 0:$data['mz_money'] = $info['mz_money'];$data['yh_money'] = $info['yh_money'];break;
            case 1:$data['discount'] = $info['discount'];$data['number'] = $info['number'];$data['zs_goods'] = $info['zs_goods'];break;
            case 2:$data['mz_money'] = $info['mz_money'];$data['zs_goods'] = $info['zs_goods'];break;
            case 3:$data['discount'] = $info['discount'];$data['zs_goods'] = $info['zs_goods'];break;
        }
        if((new Activity())->updateActivity(['activity_id'=>$info['id']],$data) === false)
		{
			return back(config('state.STATUS_CODE_TWO'),config('state.STATUS_MSG_TWO'));
		}
		return back(config('state.STATUS_CODE_ONE'),config('state.STATUS_MSG_ONE'));
    }
    /**
	* 删除
	*/
	public static function delActivity($id)
	{
		if((new Activity())->delActivity(['activity_id'=>['IN',$id]]) === false)
		{
			return back(config('state.STATUS_CODE_TWO'),config('state.STATUS_MSG_TWO'));
		}
		return back(config('state.STATUS_CODE_ONE'),config('state.STATUS_MSG_ONE'));
	}
    /**
    * 获取商品列表
    */
    public static function stockGoods($info)
    {
        $where['g.is_show'] = 0;
        if($info['sreach'])
		{
			$where['g.goods_name|g.abbreviation|g.goods_sn'] = ['like','%'.$info['sreach'].'%'];
		}
        /*已选中商品id*/
        if($info['gids'] != '')
        {
            $where['g.goods_id'] = ['NOT IN',$info['gids']];
        }
        $field = 'g.goods_id,g.goods_name,g.brand_name,g.marker,g.memory_name,g.attribute_name,g.colour_name,g.selling_price,g.trade_price,s.buying_price';
        return (new Stock())->getStockList($where,$field,10,$info['psge'],'g.goods_id');
    }
    /**
    * 获取指定商品信息
    */
    public static function stockGoodsList($goodsid)
    {
        $where['g.goods_id'] = ['IN',$goodsid];
        $field = 'g.goods_id,g.goods_name,g.brand_name,g.marker,g.memory_name,g.attribute_name,g.colour_name,g.selling_price,g.trade_price,s.buying_price';
        return (new Stock())->stockList($where,$field,'g.goods_id');
    }
}
